package com.qianbei.common.pay;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerControl.ServerListener;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.pay.PayService.PayResultListener;

import org.json.JSONObject;

public class PayActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.test_kit_pay_activity);

		findViewById(R.id.alipay).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				alipay();
			}
		});
		findViewById(R.id.wechat).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				wechat2();
			}
		});
	}

	private void alipay() {
		ServerControl sc = new ServerControl(
				"http://123.57.154.109/transaction_app/bookings/pay",
				"order_id", "763382434444", "payment_type", "1");

		sc.serverListener = new ServerListener() {
			@Override
			public void serverFinish(ServerResult sr) {
				if (sr.isContinue) {
					Message msg = new Message();
					msg.what = 0;
					msg.obj = sr.bodyData.optString("order_string");
					mainThreadHandler.sendMessage(msg);
				}

			}
		};
		new ProgressDialog(mContext).startControl(sc);
	}

	private void wechat2() {
		ServerControl sc = new ServerControl(
				"http://123.57.154.109/transaction_app/bookings/pay",
				"order_id", "763382434444", "payment_type", "2");

		sc.serverListener = new ServerListener() {
			@Override
			public void serverFinish(ServerResult sr) {
				if (sr.isContinue) {
					Message msg = new Message();
					msg.what = 1;
					msg.obj = sr.bodyData.optJSONObject("order_string");
					mainThreadHandler.sendMessage(msg);
				}

			}
		};
		new ProgressDialog(mContext).startControl(sc);
	}

	@SuppressLint("HandlerLeak")
	private Handler mainThreadHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				PayService.getInstance().alipayPay((String) msg.obj,
						new PayResultListener() {

							@Override
							public void payResult(boolean isSuccess) {
								Log.d("lz", "支付宝支付完成:" + isSuccess);
							}
						}, PayActivity.this);
			} else if (msg.what == 1) {
				PayService.getInstance().wechatPay((JSONObject) msg.obj,
						new PayResultListener() {

							@Override
							public void payResult(boolean isSuccess) {
								Log.d("lz", "微信支付完成:" + isSuccess);
							}
						}, PayActivity.this);
			}
		}
	};

	// 将输入流转换成字符串
}
